1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.primitives;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.collect.testing.Helpers;
21  
22  import junit.framework.TestCase;
23  
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Comparator;
28  import java.util.List;
29  
30  /**
31   * Unit test for {@link Booleans}.
32   *
33   * @author Kevin Bourrillion
34   */
35  @GwtCompatible(emulated = true)
36  public class BooleansTest extends TestCase {
37    private static final boolean[] EMPTY = {};
38    private static final boolean[] ARRAY_FALSE = {false};
39    private static final boolean[] ARRAY_TRUE = {true};
40    private static final boolean[] ARRAY_FALSE_FALSE = {false, false};
41    private static final boolean[] ARRAY_FALSE_TRUE = {false, true};
42  
43    private static final boolean[] VALUES = {false, true};
44  
45    public void testHashCode() {
46      assertEquals(Boolean.TRUE.hashCode(), Booleans.hashCode(true));
47      assertEquals(Boolean.FALSE.hashCode(), Booleans.hashCode(false));
48    }
49  
50    public void testCompare() {
51      for (boolean x : VALUES) {
52        for (boolean y : VALUES) {
53          // note: spec requires only that the sign is the same
54          assertEquals(x + ", " + y,
55                       Boolean.valueOf(x).compareTo(y),
56                       Booleans.compare(x, y));
57        }
58      }
59    }
60  
61    public void testContains() {
62      assertFalse(Booleans.contains(EMPTY, false));
63      assertFalse(Booleans.contains(ARRAY_FALSE, true));
64      assertTrue(Booleans.contains(ARRAY_FALSE, false));
65      assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, false));
66      assertTrue(Booleans.contains(ARRAY_FALSE_TRUE, true));
67    }
68  
69    public void testIndexOf() {
70      assertEquals(-1, Booleans.indexOf(EMPTY, ARRAY_FALSE));
71      assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE_TRUE));
72      assertEquals(0, Booleans.indexOf(ARRAY_FALSE_FALSE, ARRAY_FALSE));
73      assertEquals(0, Booleans.indexOf(ARRAY_FALSE, ARRAY_FALSE));
74      assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_FALSE));
75      assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, ARRAY_TRUE));
76      assertEquals(0, Booleans.indexOf(ARRAY_TRUE, new boolean[0]));
77    }
78  
79    public void testIndexOf_arrays() {
80      assertEquals(-1, Booleans.indexOf(EMPTY, false));
81      assertEquals(-1, Booleans.indexOf(ARRAY_FALSE, true));
82      assertEquals(-1, Booleans.indexOf(ARRAY_FALSE_FALSE, true));
83      assertEquals(0, Booleans.indexOf(ARRAY_FALSE, false));
84      assertEquals(0, Booleans.indexOf(ARRAY_FALSE_TRUE, false));
85      assertEquals(1, Booleans.indexOf(ARRAY_FALSE_TRUE, true));
86      assertEquals(2, Booleans.indexOf(new boolean[] {false, false, true}, true));
87    }
88  
89    public void testLastIndexOf() {
90      assertEquals(-1, Booleans.lastIndexOf(EMPTY, false));
91      assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE, true));
92      assertEquals(-1, Booleans.lastIndexOf(ARRAY_FALSE_FALSE, true));
93      assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE, false));
94      assertEquals(0, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, false));
95      assertEquals(1, Booleans.lastIndexOf(ARRAY_FALSE_TRUE, true));
96      assertEquals(2, Booleans.lastIndexOf(new boolean[] {false, true, true}, true));
97    }
98  
99    public void testConcat() {
100     assertTrue(Arrays.equals(EMPTY, Booleans.concat()));
101     assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY)));
102     assertTrue(Arrays.equals(EMPTY, Booleans.concat(EMPTY, EMPTY, EMPTY)));
103     assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE)));
104     assertNotSame(ARRAY_FALSE, Booleans.concat(ARRAY_FALSE));
105     assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.concat(EMPTY, ARRAY_FALSE, EMPTY)));
106     assertTrue(Arrays.equals(
107         new boolean[] {false, false, false},
108         Booleans.concat(ARRAY_FALSE, ARRAY_FALSE, ARRAY_FALSE)));
109     assertTrue(Arrays.equals(
110         new boolean[] {false, false, true},
111         Booleans.concat(ARRAY_FALSE, ARRAY_FALSE_TRUE)));
112   }
113 
114   public void testEnsureCapacity() {
115     assertSame(EMPTY, Booleans.ensureCapacity(EMPTY, 0, 1));
116     assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 0, 1));
117     assertSame(ARRAY_FALSE, Booleans.ensureCapacity(ARRAY_FALSE, 1, 1));
118     assertTrue(Arrays.equals(
119         new boolean[] {true, false, false},
120         Booleans.ensureCapacity(new boolean[] {true}, 2, 1)));
121   }
122 
123   public void testEnsureCapacity_fail() {
124     try {
125       Booleans.ensureCapacity(ARRAY_FALSE, -1, 1);
126       fail();
127     } catch (IllegalArgumentException expected) {
128     }
129     try {
130       // notice that this should even fail when no growth was needed
131       Booleans.ensureCapacity(ARRAY_FALSE, 1, -1);
132       fail();
133     } catch (IllegalArgumentException expected) {
134     }
135   }
136 
137   public void testJoin() {
138     assertEquals("", Booleans.join(",", EMPTY));
139     assertEquals("false", Booleans.join(",", ARRAY_FALSE));
140     assertEquals("false,true", Booleans.join(",", false, true));
141     assertEquals("falsetruefalse",
142         Booleans.join("", false, true, false));
143   }
144 
145   public void testLexicographicalComparator() {
146     List<boolean[]> ordered = Arrays.asList(
147         new boolean[] {},
148         new boolean[] {false},
149         new boolean[] {false, false},
150         new boolean[] {false, true},
151         new boolean[] {true},
152         new boolean[] {true, false},
153         new boolean[] {true, true},
154         new boolean[] {true, true, true});
155 
156     Comparator<boolean[]> comparator = Booleans.lexicographicalComparator();
157     Helpers.testComparator(comparator, ordered);
158   }
159 
160   public void testToArray() {
161     // need explicit type parameter to avoid javac warning!?
162     List<Boolean> none = Arrays.<Boolean>asList();
163     assertTrue(Arrays.equals(EMPTY, Booleans.toArray(none)));
164 
165     List<Boolean> one = Arrays.asList(false);
166     assertTrue(Arrays.equals(ARRAY_FALSE, Booleans.toArray(one)));
167 
168     boolean[] array = {false, false, true};
169 
170     List<Boolean> three = Arrays.asList(false, false, true);
171     assertTrue(Arrays.equals(array, Booleans.toArray(three)));
172 
173     assertTrue(Arrays.equals(array, Booleans.toArray(Booleans.asList(array))));
174   }
175 
176   public void testToArray_threadSafe() {
177     // Only for booleans, we lengthen VALUES
178     boolean[] VALUES = BooleansTest.VALUES;
179     VALUES = Booleans.concat(VALUES, VALUES);
180 
181     for (int delta : new int[] { +1, 0, -1 }) {
182       for (int i = 0; i < VALUES.length; i++) {
183         List<Boolean> list = Booleans.asList(VALUES).subList(0, i);
184         Collection<Boolean> misleadingSize =
185             Helpers.misleadingSizeCollection(delta);
186         misleadingSize.addAll(list);
187         boolean[] arr = Booleans.toArray(misleadingSize);
188         assertEquals(i, arr.length);
189         for (int j = 0; j < i; j++) {
190           assertEquals(VALUES[j], arr[j]);
191         }
192       }
193     }
194   }
195 
196   public void testToArray_withNull() {
197     List<Boolean> list = Arrays.asList(false, true, null);
198     try {
199       Booleans.toArray(list);
200       fail();
201     } catch (NullPointerException expected) {
202     }
203   }
204 
205   public void testAsListIsEmpty() {
206     assertTrue(Booleans.asList(EMPTY).isEmpty());
207     assertFalse(Booleans.asList(ARRAY_FALSE).isEmpty());
208   }
209 
210   public void testAsListSize() {
211     assertEquals(0, Booleans.asList(EMPTY).size());
212     assertEquals(1, Booleans.asList(ARRAY_FALSE).size());
213     assertEquals(2, Booleans.asList(ARRAY_FALSE_TRUE).size());
214   }
215 
216   public void testAsListIndexOf() {
217     assertEquals(-1, Booleans.asList(EMPTY).indexOf("wrong type"));
218     assertEquals(-1, Booleans.asList(EMPTY).indexOf(true));
219     assertEquals(-1, Booleans.asList(ARRAY_FALSE).indexOf(true));
220     assertEquals(0, Booleans.asList(ARRAY_FALSE).indexOf(false));
221     assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).indexOf(true));
222   }
223 
224   public void testAsListLastIndexOf() {
225     assertEquals(-1, Booleans.asList(EMPTY).indexOf("wrong type"));
226     assertEquals(-1, Booleans.asList(EMPTY).indexOf(true));
227     assertEquals(-1, Booleans.asList(ARRAY_FALSE).lastIndexOf(true));
228     assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).lastIndexOf(true));
229     assertEquals(1, Booleans.asList(ARRAY_FALSE_FALSE).lastIndexOf(false));
230   }
231 
232   public void testAsListContains() {
233     assertFalse(Booleans.asList(EMPTY).contains("wrong type"));
234     assertFalse(Booleans.asList(EMPTY).contains(true));
235     assertFalse(Booleans.asList(ARRAY_FALSE).contains(true));
236     assertTrue(Booleans.asList(ARRAY_TRUE).contains(true));
237     assertTrue(Booleans.asList(ARRAY_FALSE_TRUE).contains(false));
238     assertTrue(Booleans.asList(ARRAY_FALSE_TRUE).contains(true));
239   }
240 
241   public void testAsListEquals() {
242     assertEquals(Booleans.asList(EMPTY), Collections.emptyList());
243     assertEquals(Booleans.asList(ARRAY_FALSE), Booleans.asList(ARRAY_FALSE));
244     assertFalse(Booleans.asList(ARRAY_FALSE).equals(ARRAY_FALSE));
245     assertFalse(Booleans.asList(ARRAY_FALSE).equals(null));
246     assertFalse(Booleans.asList(ARRAY_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE)));
247     assertFalse(Booleans.asList(ARRAY_FALSE_FALSE).equals(Booleans.asList(ARRAY_FALSE_TRUE)));
248     assertEquals(1, Booleans.asList(ARRAY_FALSE_TRUE).lastIndexOf(true));
249     List<Boolean> reference = Booleans.asList(ARRAY_FALSE);
250     assertEquals(Booleans.asList(ARRAY_FALSE), reference);
251     assertEquals(reference, reference);
252   }
253 
254   public void testAsListHashcode() {
255     assertEquals(1, Booleans.asList(EMPTY).hashCode());
256     assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), Booleans.asList(ARRAY_FALSE).hashCode());
257     List<Boolean> reference = Booleans.asList(ARRAY_FALSE);
258     assertEquals(Booleans.asList(ARRAY_FALSE).hashCode(), reference.hashCode());
259   }
260 
261   public void testAsListToString() {
262     assertEquals("[false]", Booleans.asList(ARRAY_FALSE).toString());
263     assertEquals("[false, true]", Booleans.asList(ARRAY_FALSE_TRUE).toString());
264   }
265 
266   public void testAsListSet() {
267     List<Boolean> list = Booleans.asList(ARRAY_FALSE);
268     assertFalse(list.set(0, true));
269     assertTrue(list.set(0, false));
270     try {
271       list.set(0, null);
272       fail();
273     } catch (NullPointerException expected) {
274     }
275     try {
276       list.set(1, true);
277       fail();
278     } catch (IndexOutOfBoundsException expected) {
279     }
280   }
281 
282   public void testCountTrue() {
283     assertEquals(0, Booleans.countTrue());
284     assertEquals(0, Booleans.countTrue(false));
285     assertEquals(1, Booleans.countTrue(true));
286     assertEquals(3, Booleans.countTrue(false, true, false, true, false, true));
287     assertEquals(1, Booleans.countTrue(false, false, true, false, false));
288   }
289 }
290